<!DOCTYPE HTML>
<html class="x-admin-sm">
<head>
    <th:block th:insert="head/head :: common_head"></th:block>
</head>
<body onload="init('dlmc')">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card" style="margin: 10px;">
                    <div class="layui-tab layui-tab-brief">
                        <div class="layui-tab-content" >
                            <div class="layui-tab-item layui-show">
                                <form class="layui-form layui-form-pane" autocomplete="off">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>登录名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="dlmc" name="dlmc" class="layui-input" onkeydown="on_key_tab('yhmc',event);">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>用户名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="yhmc" name="yhmc" class="layui-input" onkeydown="on_key_tab_s('xzjg_id',event);">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>登录密码：
                                        </label>
                                        <div class="layui-input-block">
                                            <font color="gray">新用户密码为：111111</font>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>所属机构：
                                        </label>
                                        <div class="layui-input-block">
                                            <select name="xzjg_id" id="xzjg_id" lay-search="" lay-filter="xzjg_id">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>角色名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <select name="role_id" id="role_id" lay-search="" lay-filter="role_id">

                                            </select>
                                        </div>
                                    </div>
                                </form>
                                <button class="layui-btn" lay-submit="" lay-filter="*" id="bc">
                                    新增存盘
                                </button>
                                <button type="button" class="layui-btn layui-btn-primary" onclick="window.location.href='query?syhc=1'">返回上页</button>
                                <div style="height:100px;"></div>
                            </div>

                        </div>
                    </div>
                </div>
        </div>
    </div>
</div>
</div>
<th:block th:insert="head/head :: common_jsfiles"></th:block>
<script>
    /**
     * lx 0 表示新增  1 表示修改 id 为主键
     * 所有增改页面，载入时，都是通过doFirst回调renderForm
     *
     */
    var lx="[[${lx}]]";
    var id="[[${id}]]";
    var url = "[[${serverUrl}]]/sys/czy/";
    var ms="t_czy_add";
    $(document).ready(function() {
        if(lx=="1"){
            $("#bc").html("修改存盘");
        }else{
            $("#bc").html("新增存盘")
        }
    });
    function on_result(result)
    {
        if (result.code!=500)
        {
           if (result.code==1 || result.code==2){
                //新增成功
                layer.msg(result.msg, { time: 1000 }, function() {
                    window.location.href='query?syhc=1'
                });//失败时只提示信息，不关闭弹出层且不刷新父页
            } else {
               layer.msg(result.msg);
               $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
            }
        }
        else
        {
            layer.msg(result.msg);
            $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
        }
    }
    //表单相关
    layui.use(['form'], function(){
        var form = layui.form;

        //渲染表格参数
        var renderForm = function (formData){
            var options = '<option value=""></option>';
            //父项目
            //console.info("formData.pXmList:"+JSON.stringify(formData.pXmList));
            formData.tXzjgList.forEach(function(item) {
                options += '<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('select[id="xzjg_id"]').html(options); // 添加选项到 select 中

            options = "";
            options = '<option value=""></option>';
            formData.tRoleList.forEach(function(item) {
                options += '<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('select[id="role_id"]').html(options); // 添加选项到 select 中

            if (lx==1){
                console.info("查询得到的数据:"+JSON.stringify(formData));
                $("#dlmc").val(formData.tCzy.dlmc);
                $("#yhmc").val(formData.tCzy.name);
                $("#xzjg_id").val(formData.tCzy.xzjgId);
                $("#role_id").val(formData.tCzy.roleId);
            }
            form.render(); // 重新渲染

        }

        function doFirst(){
            //发送的请求
            var jsonData = {
                id: id,
                lx: lx,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_form(url+"info",JSON.stringify(sendStr),"数据加载中...",renderForm);
        }

        //特殊select的焦点事件
        form.on('select(xzjg_id)', function (data) {
            init_select('role_id');
        });
        form.on('select(role_id)', function (data) {
            init_select('zgxx_id');
        });
        form.on('select(zgxx_id)', function (data) {
            $("#bc").focus();
        });

        function onSave(nonce_zhi){
            //获取提交令牌
            if (is_null(nonce_zhi)){
                $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
                layer.msg("没有获取到操作凭证！请重试！",{icon:2,anim:6});
                return false;
            }
            var jsonData = {
                dlmc: $("#dlmc").val(),
                yhmc: $("#yhmc").val(),
                xzjg_id: $("#xzjg_id").val(),
                role_id: $("#role_id").val(),
                lx: lx,
                id: id,
                nonce_ms:ms,
                nonce_zhi:nonce_zhi,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_token(url+"save",JSON.stringify(sendStr),"数据提交中...");
        }

        //监听提交
        form.on('submit(*)', function(data){
            if ($("#dlmc").val()==""){
                layer.msg("请输入登录名称！",{icon:2,anim:6});
                init('dlmc');
                return false;
            }
            if ($("#yhmc").val()==""){
                layer.msg("请输入用户名称！",{icon:2,anim:6});
                init('yhmc');
                return false;
            }
            if ($("#xzjg_id").val()==""){
                layer.msg("请选择行政机构！",{icon:2,anim:6});
                init_select('xzjg_id');
                return false;
            }
            if ($("#role_id").val()==""){
                layer.msg("请选择角色名称！",{icon:2,anim:6});
                init_select('role_id');
                return false;
            }
            if (!confirm("是否确定操作？")) return false;

            $('#bc').addClass("layui-btn-disabled").attr("disabled",true);
            psot_get_nonce(ms,"[[${serverUrl}]]/getnonce",onSave)
            return false;
        });

        doFirst();
    });

</script>
</body>
</html>